<?php
namespace api\modules\v1\controllers;

use common\modules\user\models\User;
use Yii;
use yii\base\UserException;

/**
 * Class AuthController
 *
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class AuthController extends \yii\rest\Controller
{
    /**
     * POST login & password to get access_token
     * @throws UserException
     * @return multitype:NULL
     */
    public function actionAccessToken()
    {
        $login = Yii::$app->request->post('login', null);
        $password = Yii::$app->request->post('password', null);
        if ($login) {
            $user = User::findByLogin($login);
            return $user;
            if ($user) {
                if ($user->validatePassword($password)) {
                    return [
                        'id' => $user->id,
                        'access_token' => $user->access_token
                    ];
                }
            }
            throw new UserException('用户名或密码错误', 102);
        }
        else {
            throw new UserException('参数错误', 101);
        }
    }
}